<?php
//error_reporting(0);

require_once('config.php');
require_once(realpath(dirname(__FILE__)."/gsa_admin_searchreport.php"));

if(isset($argv[1])&&!empty($argv[1])){
	$collection = $argv[1];
}elseif(isset($_REQUEST['collection'])&&!empty($_REQUEST['collection'])){
	$collection = $_REQUEST['collection'];
}else{
	$collection = 'default_collection';
}

$username = "admin";
$password = "SECRET";


$ranges['lastweek'] = 7;
$ranges['last24'] = 1;
$ranges['last30days'] = 30;

foreach ($ranges as $reportName => $range){

	$gsaAdmin = new GSA_Admin(array('collection' => $collection, 'reportName' => $reportName,'dateFormat' => 'range', 'rangeDays' => $range, 'verbose' => true, "username" => $username, "password" => $password));
	
	$tries = 0;
	$report = false;
	$saved  = false;
	$login  = false;
	do {
		$gsaAdmin->logOut();
		$tries ++;
		echo "Try number $tries. \n";
		if($login = $gsaAdmin->login()){
			$gsaAdmin->getSecurityToken();
			$report = $gsaAdmin->generateReport();
			if($report !== false){
				$saved = $gsaAdmin->saveReport();
				if($saved === false){
					echo "Saving report ".$gsaAdmin->reportName." failed.\n";
				}
			}else{
				echo "Report failed\n";
			}
		}else{
			echo "Login failed\n";
		}
		
	}while(($login === false||$report === false||$saved === false)&&$tries < 10);
	$gsaAdmin = array();
}
/*
$gsaAdmin = new GSA_Admin(array('collection' => $collection, 'reportName' => 'lastweek','dateFormat' => 'range', 'rangeDays' => 7, 'verbose' => true, "username" => $username, "password" => $password));
$gsaAdmin->logOut();
if($gsaAdmin->login()){
	$report = $gsaAdmin->generateReport();
	if($report !== false){
		if(!$gsaAdmin->saveReport()){
			echo "Saving report ".$gsaAdmin->reportName." failed.\n";
		}
	}else{
		echo "Report failed\n";
	}
}else{
	echo "Login failed\n";
	echo "$login";
}
$gsaAdmin = array();
$gsaAdmin = new GSA_Admin(array('collection' => $collection, 'reportName' => 'last24','dateFormat' => 'range', 'rangeDays' => 1, 'verbose' => true, "username" => $username, "password" => $password));
$gsaAdmin->logOut();
if($gsaAdmin->login()){
	$report = $gsaAdmin->generateReport();
	if($report !== false){
		if(!$gsaAdmin->saveReport()){
			echo "Saving report ".$gsaAdmin->reportName." failed.\n";
		}
	}else{
		echo "Report failed\n";
	}
}else{
	echo "Login failed\n";
	echo "$login";
}
*/